package com.yzy.fly.loader;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yzy.fly.entity.role.*;
import com.yzy.fly.mapper.*;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 模型加载器
 */
@Component
public class FlyControllerLoader {
    @Autowired
    private FlyControllerMapper flyControllerMapper;
    @Autowired
    private FlyInterfaceMapper flyInterfaceMapper;
    @Autowired
    private FlyRequestMappingMapper flyRequestMappingMapper;

    public FlyController loader(int id) {
        return loaderRootId(id);
    }

    public FlyController loaderRootId(int id) {
        FlyController flyController = flyControllerMapper.selectById(id);
        return loader(flyController);
    }

    public FlyController loader(FlyController flyController) {
        if (flyController != null) {
            Integer controllerId = flyController.getId();

            QueryWrapper<FlyInterface> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("parentId",controllerId);
            queryWrapper.eq("componentId",flyController.getParentId());
            List<FlyInterface> flyInterfaces = flyInterfaceMapper.selectList(queryWrapper);
//            List<FlyInterface> flyInterfaces = flyInterfaceMapper.selectAllByControllerId(controllerId);

            if (CollectionUtils.isNotEmpty(flyInterfaces)) {
                flyController.setInterfaces(flyInterfaces);
                for (int i = 0; i < flyInterfaces.size(); i++) {
                    FlyInterface flyInterface =  flyInterfaces.get(i);
                    List<FlyRequestMapping> flyRequestMappings = flyRequestMappingMapper.selectAllByControllerId(flyInterface.getId());
                    flyInterface.setRequestMappings(flyRequestMappings);
                }
            }
        }
        return flyController;
    }
}
